/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "ukuisettings.h"
#include <QDebug>
#include <QTextCodec>

Settings::Settings(const QString &fileName, Format format, const QString group, QObject *parent) :
    QSettings(fileName, format, parent)
  , m_group(group)
{
    this->setIniCodec(QTextCodec::codecForName("UTF-8"));
}

QString Settings::group() const
{
    return m_group;
}

void Settings::setGroup(const QString &group)
{
    m_group = group;
}

Settings::~Settings()
{
}

QVariant Settings::value(const QString &key, const QVariant &defaultValue)
{
    QSettings::beginGroup(m_group);
    QVariant value = QSettings::value(key, defaultValue);
    QSettings::endGroup();
    return value;
}

void Settings::setValue(const QString &key, const QVariant &value)
{
    QSettings::beginGroup(m_group);
    QSettings::setValue(key, value);
    QSettings::endGroup();
    emit settingsChanged();
}

void Settings::remove(const QString &key)
{
    QSettings::beginGroup(m_group);
    QSettings::remove(key);
    QSettings::endGroup();
    emit settingsChanged();
}

bool Settings::contains(const QString &key)
{
    QSettings::beginGroup(m_group);
    bool ret = QSettings::contains(key);
    QSettings::endGroup();
    return ret;
}

QList<QMap<QString, QVariant> > Settings::readArray(const QString& prefix)
{
    QSettings::beginGroup(m_group);
    QList<QMap<QString, QVariant> > array;
    int size = QSettings::beginReadArray(prefix);
    for (int i = 0; i < size; ++i)
    {
        QSettings::setArrayIndex(i);
        QMap<QString, QVariant> map;
        const auto keys = QSettings::childKeys();
        for (const QString &key : keys) {
            map[key] = QSettings::value(key);
        }
        if (array.contains(map)) {
            continue;
        } else {
            array << map;
        }
    }
    QSettings::endArray();
    QSettings::endGroup();
    return array;
}

void Settings::setArray(const QString &prefix, const QList<QMap<QString, QVariant> > &hashList)
{
    QSettings::beginGroup(m_group);
    QSettings::beginWriteArray(prefix);
    // 清空原来的所有key/value
    QSettings::remove("");
    int size = hashList.size();
    for (int i = 0; i < size; ++i)
    {
        QSettings::setArrayIndex(i);
        QMapIterator<QString, QVariant> it(hashList.at(i));
        while (it.hasNext())
        {
            it.next();
            QSettings::setValue(it.key(), it.value());
        }
    }
    QSettings::endArray();
    QSettings::endGroup();
    emit settingsChanged();
}

void Settings::clear()
{
    QSettings::beginGroup(m_group);
    QSettings::clear();
    QSettings::endGroup();
    emit settingsChanged();
}

QStringList Settings::allKeys()
{
    QSettings::beginGroup(m_group);
    QStringList keys = QSettings::allKeys();
    QSettings::endGroup();
    return keys;
}

QStringList Settings::childGroups()
{
    QSettings::beginGroup(m_group);
    QStringList groups = QSettings::childGroups();
    QSettings::endGroup();
    return groups;
}

bool Settings::event(QEvent *event)
{
    return QSettings::event(event);
}
